// commonjs
const http = require('http')
const axios = require('axios')
const fs = require('fs')

function loadData() {
  return new Promise((resolve, reject) => {
    // ajax请求
    axios({
      url: 'https://m.maoyan.com/ajax/moreClassicList?sortId=1&showType=3&limit=10&offset=30&optimus_uuid=A5518FF0AFEC11EAAB158D7AB0D05BBBD74C9789D9F649898982E6542C7DD479&optimus_risk_level=71&optimus_code=10'
    })
    .then(res => {
      resolve(res.data)
    })
  })
}

const app = http.createServer(async (req, res) => {
  
  // 结束了
  let result = await loadData()
  
  // 文件操作
  fs.writeFile('./log.txt', result, (err) => {
    console.log('文件创建成功。')
  })
  res.end('hello')
})
app.listen(8080, () => {
  console.log('server is running at localhsot:8080')
})